package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wineshop.UserWineShopData;
import com.yanqu.road.entity.wineshop.WineShopBeautyArray;
import com.yanqu.road.entity.wineshop.WineShopRankActivityData;
import com.yanqu.road.entity.wineshop.WineShopReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/7/6 17:39
 */
public class UserWineShopDaoImpl extends TempDataObjectDao<UserWineShopData> {
    public static String insertSql = "insert into t_u_wineshop_data(userId,unlockRegion,power,lastRecoverPowerTime,waitGenerateCount," +
            "waitWineShopBeautyArrays,runningBeautyArrays,popularityValue,maxArrayId,maxBeautyId,receptionBeautyCount," +
            "lastReceptionBeautyTime,wineShopName,yieldReward,getRewardTime,changeNickNameTimes,generatedBeautyList,generatedPatronsList,wineShopLv, wineShopRankActivityDataMap) " +
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserWineShopData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnlockRegion()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWaitGenerateCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitWineShopBeautyArrays())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRunningBeautyArrays())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPopularityValue()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxArrayId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxBeautyId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReceptionBeautyCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReceptionBeautyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getWineShopName()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getYieldReward())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChangeNickNameTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getGeneratedBeautyList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getGeneratedPatronsList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWineShopLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWineShopRankActivityDataMap())));

        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wineshop_data SET unlockRegion=?,power=?,lastRecoverPowerTime=?,waitGenerateCount=?," +
            "waitWineShopBeautyArrays=?,runningBeautyArrays=?,popularityValue=?,maxArrayId=?,maxBeautyId=?,receptionBeautyCount=?," +
            "lastReceptionBeautyTime=?,wineShopName=?,yieldReward=?,getRewardTime=?,changeNickNameTimes=?,generatedBeautyList=?,generatedPatronsList=?,wineShopLv=?,wineShopRankActivityDataMap=? WHERE userId = ?";
    @Override
    protected boolean updateData(UserWineShopData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnlockRegion()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWaitGenerateCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWaitWineShopBeautyArrays())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRunningBeautyArrays())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPopularityValue()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxArrayId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxBeautyId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReceptionBeautyCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReceptionBeautyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getWineShopName()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getYieldReward())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChangeNickNameTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getGeneratedBeautyList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getGeneratedPatronsList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWineShopLv())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWineShopRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));

        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWineShopData parseData(ResultSet rs) throws SQLException {
        UserWineShopData data = new UserWineShopData();
        data.setUserId(rs.getLong("userId"));
        data.setUnlockRegion(rs.getInt("unlockRegion"));
        data.setPower(rs.getInt("power"));
        data.setLastRecoverPowerTime(rs.getLong("lastRecoverPowerTime"));
        data.setWaitGenerateCount(rs.getLong("waitGenerateCount"));
        data.setWaitWineShopBeautyArrays(JSONObject.parseObject(rs.getString("waitWineShopBeautyArrays"), new TypeReference<List<WineShopBeautyArray>>() {
        }));
        data.setRunningBeautyArrays(JSONObject.parseObject(rs.getString("runningBeautyArrays"), new TypeReference<List<WineShopBeautyArray>>() {
        }));
        data.setPopularityValue(rs.getLong("popularityValue"));
        data.setMaxArrayId(rs.getLong("maxArrayId"));
        data.setMaxBeautyId(rs.getLong("maxBeautyId"));
        data.setReceptionBeautyCount(rs.getLong("receptionBeautyCount"));
        data.setLastReceptionBeautyTime(rs.getLong("lastReceptionBeautyTime"));
        data.setWineShopName(rs.getString("wineShopName"));
        data.setYieldReward(JSONObject.parseObject(rs.getString("yieldReward"), new TypeReference<WineShopReward>() {
        }));
        data.setGetRewardTime(rs.getLong("getRewardTime"));
        data.setChangeNickNameTimes(rs.getInt("changeNickNameTimes"));
        data.setGeneratedBeautyList(JSONObject.parseObject(rs.getString("generatedBeautyList"), new TypeReference<List<Integer>>(){
        }));
        data.setGeneratedPatronsList(JSONObject.parseObject(rs.getString("generatedPatronsList"), new TypeReference<List<Integer>>(){
        }));
        data.setWineShopLv(rs.getInt("wineShopLv"));
        String rsString = rs.getString("wineShopRankActivityDataMap");
        if (rsString == null || rsString.isEmpty()) {
            data.setWineShopRankActivityDataMap(new ConcurrentHashMap<>());
        } else {
            data.setWineShopRankActivityDataMap(JSONObject.parseObject(rsString, new TypeReference<ConcurrentHashMap<Integer, WineShopRankActivityData>>(){}));
        }
        return data;
    }

    public UserWineShopData getUserWineShopData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserWineShopData userWineShopData = null;
        String sql = "select * from t_u_wineshop_data where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userWineShopData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userWineShopData;
    }
}
